/*
 * 人人信用管理有限公司版权所有.
 * 
 * @Description:
 * 
 * @author: liyuliang
 * 
 * @date: 20 Apr 2017 10:44:16 am
 * 
 * 注意：本内容仅限于人人信用管理有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.rrx360.common.config.global;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Properties;

import org.apache.commons.configuration.Configuration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.rrx360.common.config.Config;
import com.rrx360.common.config.ConfigChangeListener;
import com.rrx360.common.config.ConfigNode;
import com.rrx360.common.exception.RException;

public class GlobalConfig {

  public static final Logger logger = LogManager.getLogger(GlobalConfig.class);

  private static Config globalConfig;

  public static synchronized void addConfigNode(ConfigNode configNode) {
    if (globalConfig == null) {
      globalConfig = new Config();
    }
    globalConfig.addConfigNode(configNode);
  }

  public static void load() {
    globalConfig.load();
  }

  public static void addConfigChangeListener(String key, ConfigChangeListener listener) {
    globalConfig.addConfigChangeListener(key, listener);
  }

  public static synchronized void configWithDefaultStrategy() {
    GlobalConfig.addConfigNode(DefaultAppConfigNodeGetter.instance.get());
    GlobalConfig.addConfigNode(DefaultModuleConfigNodeGetter.instance.get());
    GlobalConfig.addConfigNode(DefaultCommonConfigNodeGetter.instance.get());
    GlobalConfig.load();
    GlobalConfig.getGlobalConfig().printAll();
  }

  public static void takeSnapshot(String path) {
    Configuration config = globalConfig.getConfiguration();
    BufferedWriter writer = null;
    try {
      writer = new BufferedWriter(
          new OutputStreamWriter(new FileOutputStream(path), Charset.defaultCharset()));
      if (config == null) {
        writer.write("null");
        return;
      }
      Iterator<String> keys = config.getKeys();
      while (keys.hasNext()) {
        String key = keys.next();
        String value = config.getString(key);
        if (key.contains("password") || key.contains("token")) {
          if (value.length() > 2) {
            value = value.charAt(0) + "*" + value.charAt(value.length() - 1);
          } else {
            value = "*";
          }
        }
        writer.write(key + "=" + value);
        writer.newLine();
      }
    } catch (IOException e) {
      throw new RException(e);
    } finally {
      if (writer != null) {
        try {
          writer.close();
        } catch (IOException e) {
          throw new RException(e);
        }
      }
    }

  }

  public static Config getGlobalConfig() {
    return globalConfig;
  }

  public static void setGlobalConfig(Config globalConfig) {
    GlobalConfig.globalConfig = globalConfig;
  }

  public static String get(String key) {
    if (globalConfig == null) {
      configWithDefaultStrategy();
    }
    return globalConfig.getString(key);
  }

  public static String get(String key, String defaultValue) {
    if (globalConfig == null) {
      configWithDefaultStrategy();
    }
    return globalConfig.getString(key, defaultValue);
  }

  public static Configuration getConfiguration() {
    if (globalConfig == null) {
      configWithDefaultStrategy();
    }
    return globalConfig.getConfiguration();
  }

  public static Properties getAllProperties() {
    if (globalConfig == null) {
      configWithDefaultStrategy();
    }
    return globalConfig.getAllProperties();
  }

  public static void main(String[] args) {
    // addConfigNode(DefaultCommonConfigNodeGetter.instance.get());
    // load();
    // globalConfig.printAll();
    System.out.println(GlobalConfig.get("env"));
  }

}
